package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import com.github.pagehelper.Page;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.TbChargingpilesstation;
import com.ruoyi.system.mapper.TbChargingpilesstationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TransactionrecordsMapper;
import com.ruoyi.system.domain.Transactionrecords;
import com.ruoyi.system.service.ITransactionrecordsService;

/**
 * 交易记录Service业务层处理
 * 
 * @author yy
 * @date 2024-08-13
 */
@Service
public class TransactionrecordsServiceImpl implements ITransactionrecordsService 
{
    @Autowired
    private TransactionrecordsMapper transactionrecordsMapper;
    @Autowired
    private TbChargingpilesstationMapper tbChargingpilesstationMapper;

    /**
     * 查询交易记录
     * 
     * @param trid 交易记录主键
     * @return 交易记录
     */
    @Override
    public Transactionrecords selectTransactionrecordsByTrid(Long trid)
    {
        return transactionrecordsMapper.selectTransactionrecordsByTrid(trid);
    }

    /**
     * 查询交易记录列表
     * 
     * @param transactionrecords 交易记录
     * @return 交易记录
     */
    @Override
    public List<Transactionrecords> selectTransactionrecordsList(Transactionrecords transactionrecords)
    {
        Long uid = SecurityUtils.getLoginUser().getUserId();
        if (!SecurityUtils.isAdmin(uid)) {
            List<Long> sids = tbChargingpilesstationMapper.getsidByUid(uid);
            HashMap<String, Object> map = new HashMap<>();
            map.put("sids",sids);
            transactionrecords.setParams(map);
        }
        PageUtils.startPage();
        List<Transactionrecords> trs = transactionrecordsMapper.selectTransactionrecordsList(transactionrecords);
        return trs;
    }

    /**
     * 新增交易记录
     * 
     * @param transactionrecords 交易记录
     * @return 结果
     */
    @Override
    public int insertTransactionrecords(Transactionrecords transactionrecords)
    {
        return transactionrecordsMapper.insertTransactionrecords(transactionrecords);
    }

    /**
     * 修改交易记录
     * 
     * @param transactionrecords 交易记录
     * @return 结果
     */
    @Override
    public int updateTransactionrecords(Transactionrecords transactionrecords)
    {
        return transactionrecordsMapper.updateTransactionrecords(transactionrecords);
    }

    /**
     * 批量删除交易记录
     * 
     * @param trids 需要删除的交易记录主键
     * @return 结果
     */
    @Override
    public int deleteTransactionrecordsByTrids(Long[] trids)
    {
        return transactionrecordsMapper.deleteTransactionrecordsByTrids(trids);
    }

    /**
     * 删除交易记录信息
     * 
     * @param trid 交易记录主键
     * @return 结果
     */
    @Override
    public int deleteTransactionrecordsByTrid(Long trid)
    {
        return transactionrecordsMapper.deleteTransactionrecordsByTrid(trid);
    }

    public BigDecimal calculateChargingAmountForToday() {
        LocalDate today = LocalDate.now();
        return transactionrecordsMapper.sumChargeCapacityByKindAndDate("Payment", today);
    }

    public int calculateChargingCountForToday() {
        LocalDate today = LocalDate.now();
        return transactionrecordsMapper.countTransactionsByKindAndDate("Payment", today);
    }

    public BigDecimal calculateRechargingAmountForToday() {
        LocalDate today = LocalDate.now();
        return transactionrecordsMapper.sumChargeCapacityByKindAndDate("Recharge", today);
    }

    public int calculateRechargingCountForToday() {
        LocalDate today = LocalDate.now();
        return transactionrecordsMapper.countTransactionsByKindAndDate("Recharge", today);
    }
}
